package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TBook;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TBookDao;
import com.ruipengkj.service.TBookService;

@Service
public class TBookServiceImpl implements TBookService {

	@Resource
	private TBookDao TBookDao;// DAO层

	// 根据条件查询
	@Override
	public List<TBook> findService(TBook entity) throws Exception {
		String hql = "select o from TBook o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getPosID() != null) {
				whereHQL.append("posID=?").append(BaseQuery.AND);
				param.add(entity.getPosID());
			} else {
				whereHQL.append("o.posID is null").append(BaseQuery.AND);
			}
			if (entity.isShow()) {
				whereHQL.append("(o.status=?").append(BaseQuery.OR);
				param.add(1);
				whereHQL.append("o.status=?").append(BaseQuery.OR);
				param.add(2);
				whereHQL.append("o.status=?)").append(BaseQuery.AND);
				param.add(3);
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TBookDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// 根据条件查询
	@Override
	public List<TBook> findIdService(TBook entity) throws Exception {
		String hql = "select new TBook(o.bookID) from TBook o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (entity.getPosID() != null) {
				whereHQL.append("posID=?").append(BaseQuery.AND);
				param.add(entity.getPosID());
			} else {
				whereHQL.append("o.posID is null").append(BaseQuery.AND);
			}
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TBookDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	// //根据条件删除
	// @Override
	// public boolean deleteService(TBook entity) throws Exception {
	// String hql = "select o from TBillDetail o";
	// StringBuffer whereHQL = new StringBuffer();
	// List<Object> param = new ArrayList<Object>();
	// if (entity != null) {
	// if(entity.getBillType()!=0){
	// whereHQL.append("BillType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
	// param.add("%"+entity.getBillType()+"%");
	// }
	// if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
	// whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
	// param.add("%"+entity.getBillID()+"%");
	// }
	// if(entity.getCommodityID()!=null&&!entity.getCommodityID().equals("")){
	// whereHQL.append("CommodityID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
	// param.add("%"+entity.getCommodityID()+"%");
	// }
	// if(whereHQL.length()>4)
	// whereHQL.delete(whereHQL.length()-5, whereHQL.length());
	// }
	// List<TBook> list=TBookDao.findDao(hql, whereHQL.toString(), param, null);
	// if(list!=null&&list.size()!=0){
	// for(int i = list.size()-1; i >= 0 ; --i) {
	// TBook com=list.get(i);
	// if(com!=null){
	// TBookDao.deleteDao(com.getDetailID());
	// }
	// }
	// return true;
	// }
	// return false;
	// }

	// 根据ID翻页查询
	@Override
	public PageModel<TBook> getScrollService(int firstResult, int maxResult,
			TBook entity) throws Exception {
		String hql = "select o from TBook o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if (whereHQL.length() > 4)
				whereHQL.delete(whereHQL.length() - 5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TBookDao.getScrollDao(hql, whereHQL.toString(), param,
				firstResult, maxResult, orderby);
	}

	// 修改
	@Override
	public void modifyService(TBook entity) throws Exception {
		TBookDao.modifyDao(entity);

	}

	// 添加
	@Override
	public void saveService(TBook entity) throws Exception {
		TBookDao.saveDao(entity);
	}

	// 根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TBookDao.deleteDao(id);

	}

	// 根据ID查询
	@Override
	public TBook getByIdService(String id) throws Exception {
		return TBookDao.getByIdDao(id);
	}

	// 添加或修改
	@Override
	public void saveOrUpdateService(TBook entity) throws Exception {
		TBookDao.saveOrUpdateDao(entity);
	}

	// 添加返回ID
	@Override
	public Serializable saveRetService(TBook entity) throws Exception {
		return TBookDao.saveRetDao(entity);
	}

	public TBookDao gettBillDetailDao() {
		return TBookDao;
	}

	public void settBillDetailDao(TBookDao TBookDao) {
		this.TBookDao = TBookDao;
	}

}
